<template>
  <a-modal
    :title="title"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-time-picker format="HH:mm" valueFormat="HH:mm" v-decorator="['start', { rules: [{ required: true, message: '请选择开始时间' }] }]" />
        </a-form-item>
        <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
           <a-time-picker format="HH:mm" valueFormat="HH:mm" v-decorator="['end', { rules: [{ required: true, message: '请选择结束时间' }] }]" />
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" extra="数字越小越靠前">
          <a-input-number
            :min="0"
            v-decorator="['sort', { initialValue: 10, rules: [{ required: true, message: '请输入至少1个数字' }] }]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import * as Api from '@/api/time/template'
import moment from 'moment'

export default {
  props: {},
  data() {
    return {
      // 对话框标题
      title: '',
      // 标签布局属性
      labelCol: {
        span: 7,
      },
      // 输入框布局属性
      wrapperCol: {
        span: 13,
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {}
    }
  },
  methods: {
    /**
     * 显示对话框
     */
    edit(record) {
      this.title = '编辑时间段'
      this.visible = true

      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    /**
     * 设置默认值
     */
    setFieldsValue () {
      const { form: { setFieldsValue } } = this
      this.$nextTick(() => {
        const data = _.pick(this.record, ['start', 'end', 'sort'])
        const start = data.start.split(':')
        const end = data.end.split(':')
        setFieldsValue({
          start: moment().hour(start[0]).minute(start[1]),
          end: moment().hour(end[0]).minute(end[1]),
          sort: data.sort
        })
      })
    },

    /**
     * 确认按钮
     * TODO: 时段判断，编辑的时段是否和已存在的时段冲突
     */
    handleSubmit(e) {
      e.preventDefault()
      const {
        form: { validateFields },
      } = this
      // 表单验证
      validateFields((errors, values) => {
        let start = values.start
        let end = values.end
        if(typeof start == 'object') {
          start = this.getMomentStr(start)
        }

        if(typeof end == 'object') {
          end = this.getMomentStr(end)
        }

        !errors && this.onFormSubmit({
          ...values,
          start,
          end
        })
      })
    },

    // 获取moment对象的值
    getMomentStr(obj) {
      return obj.format('HH') + ':' + obj.format('mm')
    },

    /**
    * 提交到后端api
    */
    onFormSubmit (values) {
      this.confirmLoading = true
      const timeFragmentId = this.record.time_fragment_id
      Api.editFragment({...values, 'time_fragment_id': timeFragmentId})
        .then((result) => {
           // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally((result) => {
          this.confirmLoading = false
        })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },
  },
}
</script>
